<?php

declare (strict_types=1);

namespace App\Model;

use Psr\SimpleCache\InvalidArgumentException;

/**
 * @property int $id
 * @property int $uid
 * @property int $gender
 * @property int $age
 * @property int $birthday
 * @property int $country
 * @property int $language
 * @property int $source
 * @property int $platform
 * @property string $token
 * @property string $head
 * @property string $nickname
 */
class UserDatum extends Model
{
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'user_datum';
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $guarded = [];
    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = ['id' => 'integer', 'uid' => 'integer', 'gender' => 'integer', 'age' => 'integer', 'birthday' => 'integer', 'country' => 'integer', 'language' => 'integer', 'source' => 'integer', 'platform' => 'integer'];

    /**
     * 返回用户的基本信息，做了一点缓存一个小时，如需改动请刷新redis
     * @param $uid
     * @return array|false|mixed|string
     * @throws InvalidArgumentException
     */
    public static function userInfo($uid)
    {



        $userRedisKey = 'user:userinfo:' . $uid;

        $info = cache()->get($userRedisKey);

        if (is_null($info)) {
            $info = self::from('user_datum as a')
                ->leftJoin('user as b', 'a.uid', '=', 'b.id')
                ->leftJoin('country as c', 'a.country', '=', 'c.id')
                ->leftJoin('language as d', 'a.language', '=', 'd.id')
                ->where(['a.uid' => $uid,'a.status'=>1])
                ->select(['a.*', 'b.num', 'b.role', 'b.become_due as becomeDue','a.status as datumStatus', 'b.type as userType', 'c.self_name as countryName', 'c.code as countryCode', 'd.name as languageName', 'd.code as languageCode'])
                ->first();

            if ($info) {
                $info = $info->toArray();
                //用户信息缓存 一个小时
                cache()->set($userRedisKey, $info, 3600);
            } else {
                $info = null;
            }
        }
        return $info;

    }

    public static function consultantUserInfo($uid){
        $userRedisKey = 'consultantUser:userinfo:' . $uid;

        $info = cache()->get($userRedisKey);

        if (is_null($info)) {
            $info = self::from('user_datum as a')
                ->leftJoin('user as b', 'a.uid', '=', 'b.id')
                ->leftJoin('country as c', 'a.country', '=', 'c.id')
                ->leftJoin('language as d', 'a.language', '=', 'd.id')
                ->where(['a.uid' => $uid])
                ->select(['a.*', 'b.num', 'b.role', 'b.become_due as becomeDue','a.status as datumStatus', 'b.type as userType', 'c.self_name as countryName', 'c.code as countryCode', 'd.name as languageName', 'd.code as languageCode'])
                ->first();

            if ($info) {
                $info = $info->toArray();
                //用户信息缓存 一个小时
                cache()->set($userRedisKey, $info, 3600);
            } else {
                $info = null;
            }
        }
        return $info;
    }




    public static function addUser($uid)
    {

        $insertData = [
            'uid' => $uid,
            'status' => 0,
        ];
        $rel = self::insert($insertData);

        var_dump($rel);

    }
}
